// import 'package:ali_auth/ali_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_demotest/api/index.dart';
import 'package:flutter_demotest/models/login.model.dart';
import 'package:flutter_demotest/store/user.dart';
// import '../login_page_config.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import '../models/user_storm_info.model.dart';

class LoginViewModel with ChangeNotifier {
  final LoginModel _loginModel = LoginModel(phone: '', uuid: '', captcha: '');

  Map<String, dynamic> get loginForm => {'phone': _loginModel.phone, 'captcha': _loginModel.captcha};

  void setPhone(String phone) {
    _loginModel.phone = phone;
    notifyListeners();
  }

  void setCaptcha(String captcha) {
    _loginModel.captcha = captcha;
    notifyListeners();
  }

  void getCaptcha() async {
    final captchaInfo = await ApiService()
        .post('/frontCaptcha', data: {'phone': _loginModel.phone});
    // SmartDialog.showToast(
    //   captchaInfo.data['msg'],
    //   alignment: Alignment.topCenter,
    // );
    _loginModel.uuid = captchaInfo.data['uuid'];
    debugPrint('$captchaInfo');
    notifyListeners();
  }

  void login(context) async {
    SmartDialog.showLoading(msg: '登录中...');
    String regexPhoneNumber =
        r'^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$';
    RegExp phoneRegExp = RegExp(regexPhoneNumber);
    if (_loginModel.phone.isEmpty) {
      SmartDialog.dismiss();
      SmartDialog.showToast(
        '请输入手机号',
        alignment: Alignment.topCenter,
      );
      return;
    }

    if (!phoneRegExp.hasMatch(_loginModel.phone)) {
      SmartDialog.dismiss();
      SmartDialog.showToast(
        '手机号格式错误',
        alignment: Alignment.topCenter,
      );
      return;
    }
    if (_loginModel.captcha.isEmpty || _loginModel.captcha.length < 4) {
      SmartDialog.dismiss();
      SmartDialog.showToast(
        '请输入4位验证码',
        alignment: Alignment.topCenter,
      );
      return;
    }
    if (!_loginModel.uuid.isNotEmpty) {
      SmartDialog.dismiss();
      SmartDialog.showToast(
        '请重新获取验证码',
        alignment: Alignment.topCenter,
      );
      return;
    }
    final res = await ApiService()
        .post('/system/info/login', data: _loginModel.toMap());
    if (res.data['code'] >= 300) {
      SmartDialog.dismiss();
      SmartDialog.showToast(
        res.data['msg'],
        alignment: Alignment.topCenter,
      );
      return;
    }
    debugPrint('$res');
    UserInfoModel userInfo =
        UserInfoModel(token: res.data['token'], phone: _loginModel.phone);
    UserStore.saveUserInfo(userInfo);
    SmartDialog.dismiss();
    Navigator.pushNamedAndRemoveUntil(
      context,
      res.data['status'] == 0 ? '/refineUserInfo' : '/',
      (route) => false,
    );
  }

  void autoLogin(context) async {
    Navigator.pushNamed(context, '/loginTo');
    // if (!await NetworkManager().hasConnection()) {
    //   SmartDialog.showToast(
    //       '请检查网络连接',
    //     alignment: Alignment.topCenter,
    //   );
    //   return;
    // }
    // if(!await NetworkManager().isMobileData()){
    //   SmartDialog.showToast(
    //       '请打开流量并关闭WIFI',
    //     alignment: Alignment.topCenter,
    //   );
    //   return;
    // }
    // else{
    //   AliAuth.initSdk(aliAuthModel);
    // }
  }

  void weChatLogin() {
    SmartDialog.showToast(
      '当前登录不可用',
      alignment: Alignment.topCenter,
    );
  }
}
